package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/9/20
 * Time: 15:50
 */
public class DrawPage extends Utils {
    public static String url = "http://101.42.27.228:8080/admin.html";

    public DrawPage() {
        super(url);
    }

    /**
     * 页面从活动中心打开
     */
    public void checkDrawPage() {
        locationSubpage("//*[@id=\"activitiesList\"]");
        driver.findElement(By.xpath("//*[@id=\"activities\"]/div[1]/div[2]/p/a")).click();

        String curWindow = driver.getWindowHandle();
        Set<String> windows = driver.getWindowHandles();
        for (String window : windows) {
            if (window != curWindow) {
                driver.switchTo().window(window);
            }
        }

        driver.findElement(By.xpath("//*[@id=\"title\"]"));
        String last = driver.findElement(By.xpath("//*[@id=\"container\"]/div[4]/span[1]")).getText();
        String begin = driver.findElement(By.xpath("//*[@id=\"container\"]/div[4]/span[2]")).getText();

        String expectLast = "查看上一奖项";
        String expectBegin = "开始抽奖";

        assert last.equals(expectLast);
        assert begin.equals(expectBegin);
    }

    /**
     * 开始抽奖/确认抽奖可以进行当前奖项抽奖
     */
    public void checkCurDrawPrize() {
        WebElement begin = driver.findElement(By.xpath("//*[@id=\"container\"]/div[4]/span[2]"));
        assert begin.getText().equals("开始抽奖");
        begin.click();

        WebElement end = driver.findElement(By.xpath("//*[@id=\"container\"]/div[4]/span[2]"));
        assert end.getText().equals("点我确定");
        end.click();

        WebElement over = driver.findElement(By.xpath("//*[@id=\"container\"]/div[4]/span[2]"));
        assert over.getText().equals("已抽完，下一步");
    }

    /**
     * 可以进行下一项抽奖
     */
    public void checkNextDraw() {
        WebElement next = driver.findElement(By.xpath("//*[@id=\"container\"]/div[4]/span[2]"));
        assert next.getText().equals("已抽完，下一步");
        next.click();

    }

    /**
     * 可以查看已抽中的上一项奖项
     */
    public void checkPreDraw() {
        WebElement last = driver.findElement(By.xpath("//*[@id=\"container\"]/div[4]/span[1]"));
        assert last.getText().equals("查看上一奖项");
        last.click();
        driver.findElement(By.xpath("//*[@id=\"container\"]/div[4]/span[2]")).click();
        WebElement next = driver.findElement(By.xpath("//*[@id=\"container\"]/div[4]/span[2]"));
        assert next.getText().equals("已抽完，下一步");
    }
}
